#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QLabel>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 设置窗口标题
    setWindowTitle(QString("Russian Blocks"));
    // 临时信息，如一般的提示信息，上面讲到的动作提示就是临时信息；
    // 正常信息，如显示页数和行号；永久信息，如显示版本号或者日期
    // showMessage()函数来显示一个临时消息，它会出现在状态栏的最左边
    // ui->statusBar->showMessage(tr("Gaming!"), 2000);

//    // addWidget()函数添加一个QLabel到状态栏上用于显示正常信息
//    statusLabel = new QLabel;
//    statusLabel->setMinimumSize(100, 20);         // 设置标签最小大小
//    statusLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状
//    statusLabel->setFrameShadow(QFrame::Sunken);  // 设置标签阴影
//    ui->statusBar->addWidget(statusLabel);
//    statusLabel->setText(tr("俄罗斯方块"));
//    // 显示永久信息，要使用addPermanentWidget()函数来添加一个如QLabel一样的可以显示信息的部件
//    // 它会生成在状态栏的最右端，不会被临时消息所掩盖
//    permanent = new QLabel;
//    permanent->setFrameStyle(QFrame::Box | QFrame::Sunken);
//    permanent->setText(tr("<a href=\"https://chenxfeng.github.io\">chenxfeng.github.io</a>"));
//    permanent->setTextFormat(Qt::RichText);
//    permanent->setOpenExternalLinks(true);
//    ui->statusBar->addPermanentWidget(permanent);

    createNewGame(&russian_block, ui->openGLWidget->getCanvas());

    QObject::connect(russian_block, SIGNAL(flush()), this, SLOT(doFlush()));
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::doFlush() {
    ui->openGLWidget->update();
}

void MainWindow::keyPressEvent(QKeyEvent *e) {
    // todo: shortcut keys
    ///pass to the running game
    if (russian_block->getGameState() == ctl_types::gaming_tag::playing_game ||
        russian_block->getGameState() == ctl_types::gaming_tag::stopped_game)
        russian_block->keyPress(e);
}

void MainWindow::keyReleaseEvent(QKeyEvent *e) {
    // todo: shortcut keys
    ///pass to the running game
    if (russian_block->getGameState() == ctl_types::gaming_tag::playing_game ||
        russian_block->getGameState() == ctl_types::gaming_tag::stopped_game)
        russian_block->keyRelease(e);
}

void MainWindow::on_actionstart_triggered() {
    ///start gaming
    russian_block->setGameState(ctl_types::gaming_tag::playing_game);
    ui->openGLWidget->setGameState(ctl_types::gaming_tag::playing_game);
    /// prepare for a new game
    russian_block->newGame();
    russian_block->start();

    ui->openGLWidget->update();
}

void MainWindow::on_actionstop_triggered() {
    if (russian_block->getGameState() != ctl_types::gaming_tag::playing_game) return ;
    /// stop gaming
    russian_block->setGameState(ctl_types::gaming_tag::stopped_game);
    ui->openGLWidget->setGameState(ctl_types::gaming_tag::stopped_game);
    russian_block->terminate();

    ui->openGLWidget->update();
}

void MainWindow::on_actionend_triggered() {
    // finish gaming
    russian_block->setGameState(ctl_types::gaming_tag::game_over);
    ui->openGLWidget->setGameState(ctl_types::gaming_tag::game_over);
    russian_block->terminate();

//    QMessageBox box;
//    box.setWindowTitle(tr("Game Over"));
//    box.setIcon(QMessageBox::Warning);
//    box.setText(tr(" 游戏结束，再来一局？"));
//    QPushButton *yesBtn = box.addButton(tr("是(&Y)"), QMessageBox::YesRole);
//    box.addButton(tr("否(&N)"), QMessageBox::NoRole);
//    QPushButton *cancelBut = box.addButton(tr("取消"), QMessageBox::RejectRole);
//    box.exec();
//    if (box.clickedButton() == yesBtn) {
//        qDebug() << "cat";
//    } else if (box.clickedButton() == cancelBut) {
//        qDebug() << "dog";
//    }

    ///clear canvas data
    ui->openGLWidget->getCanvas()->clear();
    /// prepare for a new game
    russian_block->newGame();

    ui->openGLWidget->update();
}

void MainWindow::closeEvent(QCloseEvent *event) {
    /// terminate the game thread
    russian_block->setGameState(ctl_types::gaming_tag::game_over);
    if (russian_block->isRunning())
        russian_block->terminate();
    qDebug() << "Close.";
}

void MainWindow::on_actionrandom_triggered() {
    if (russian_block->getGameState() != ctl_types::gaming_tag::game_over) return ;
    // choose random block generator
    dynamic_cast<RussianBlock*>(russian_block)->setBlockGenerator(rb_types::generator_type::random);
}

void MainWindow::on_actionroll_triggered() {
    if (russian_block->getGameState() != ctl_types::gaming_tag::game_over) return ;
    // choose rolling block generator
    dynamic_cast<RussianBlock*>(russian_block)->setBlockGenerator(rb_types::generator_type::rolling);
}
